package com.lsf.dunlian.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lsf.dunlian.R;
import com.lsf.dunlian.activity.AttendanceActivity;
import com.lsf.dunlian.activity.LoginActivity;
import com.lsf.dunlian.activity.MemberActivity;
import com.lsf.dunlian.activity.NoticeActivity;
import com.lsf.dunlian.activity.ReimbursementActivity;
import com.lsf.dunlian.activity.ReimbursementRecordActivity;
import com.lsf.dunlian.adapter.GroupChatRecycleViewAdapter;
import com.lsf.dunlian.adapter.SessionRecyclerViewAdapter;
import com.lsf.dunlian.callback.GroupChatCallBack;
import com.lsf.dunlian.config.PreferenceUtil;
import com.lsf.dunlian.config.Urls;
import com.lsf.dunlian.constant.Constant;
import com.lsf.dunlian.entity.GroupChatMessageVo;
import com.lsf.dunlian.entity.UserData;
import com.lsf.dunlian.entity.UserInfoVo;
import com.lsf.dunlian.entity.UserVO;
import com.lsf.dunlian.netty.SimpleClientHandler;
import com.makeramen.roundedimageview.RoundedImageView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MyPageFragment extends Fragment implements GroupChatCallBack {
    private RoundedImageView icon;
    private TextView name;
    private TextView gangwei;
    private RelativeLayout qiehuanzhanghu;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, null);
        init(view);
        SimpleClientHandler.register(this);

        return view;
    }

    private void init(View view) {

        icon = view.findViewById(R.id.icon);
        name = view.findViewById(R.id.name);
        gangwei = view.findViewById(R.id.gangwei);
        qiehuanzhanghu = view.findViewById(R.id.qiehuanzhanghu);
        qiehuanzhanghu.setOnClickListener(v->startActivity(new Intent(getActivity(), LoginActivity.class)));

        getUserInfo();
    }

    private void getUserInfo() {
        OkHttpClient client = new OkHttpClient();
        FormBody.Builder formBodyBuilder = new FormBody.Builder();
        formBodyBuilder.add("userId", PreferenceUtil.getSharedPreference(Constant.USERID));
        Request request = new Request.Builder()
                .url(Urls.USER_INFO)
                .post(formBodyBuilder.build())
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                UserInfoVo vo = new Gson().fromJson(result, UserInfoVo.class);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        name.setText(vo.data.getName());
                        gangwei.setText(vo.data.getBackCardName());
                      //  Glide.with(getActivity()).load(vo.data.getIcon()).into(icon);
                    }
                });
            }
        });
    }

    private void getUserList() {

    }

    @Override
    public void message(GroupChatMessageVo vo) {

    }
}
